//
//  PastViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/3/22.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "PastActivityViewController.h"
#import "ActivityCell.h"
#import "ActivityInfoViewController.h"

@interface PastActivityViewController ()<UITableViewDelegate,UITableViewDataSource>

// 视图
@property(nonatomic,retain)UITableView *tableView;

// 数据
@property (nonatomic, retain)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger page;

@end

@implementation PastActivityViewController

- (instancetype)init {
    if (self=[super init]) {
        self.title = @"往期活动";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.初始化tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];

    // 2.下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 3.1 获取活动数据
        self.page = 0;
        [self updateActivityDatePage:self.page getCount:6];
    }];
    [self.tableView.mj_header beginRefreshing];

    // 3.上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 获取活动数据
        self.page++;
        [self updateActivityDatePage:self.page getCount:6];
    }];
}

#pragma mark - 网络请求
-(void)updateActivityDatePage:(NSInteger)page getCount:(NSInteger)count {
    // 判断是否为下拉刷新，如果是，重新获取数据
    if (page == 0) {
        // 清空数据
        self.dataArray = [[NSMutableArray alloc] init];
    }

    // 初始化请求参数
    NSDictionary *parameters = @{@"page": @(page), @"count": @(count)};

    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/pastActivity"
                                        parameters:parameters
                                           success:^(NSDictionary *respondDictionary) {
                                                // 1.生成模型
                                               for (NSDictionary *dic in respondDictionary[@"Activities"]) {
                                                   ActivityModel *model = [[ActivityModel alloc] initWithDictionary:dic];
                                                   [self.dataArray addObject:model];
                                               }
                                               [self.tableView reloadData];

                                               // 2.检查模型个数,以结束footer刷新
                                               if ([respondDictionary[@"Activities"] count] < count) {
                                                   // 已经没有更多数据
                                                   [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                               } else {
                                                   // 正常结束刷新
                                                   [self.tableView.mj_footer endRefreshing];
                                               }

                                               // 4.结束header刷新
                                               [self.tableView.mj_header endRefreshing];
                                           } failure:^(NSError *error) {
                
                [self updateFailed];
            }];


}

#pragma mark - 自定义方法
-(void)updateFailed {
    [SVProgressHUD showfailed];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}

#pragma mark - tableView回调方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView cellHeightForIndexPath:indexPath model:self.dataArray[indexPath.row] keyPath:@"model" cellClass:[ActivityCell class] contentViewWidth:SCREEN_WIDTH];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    if (cell==nil) {
        cell = [[ActivityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
    }

    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];

    ActivityCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    //显示控制器
    ActivityInfoViewController *activityInfoViewController = [[ActivityInfoViewController alloc] init];
    activityInfoViewController.activityID = cell.model.activity_id;

    [self.navigationController pushViewController:activityInfoViewController animated:true];
}


@end
